﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Soundelle.Oscillator
{
   public class FilterStairs : BaseDevice
    {
        private ParameterType[] _AvailableParameters = { 
            ParameterType.Parameter_DC_Offset,
            ParameterType.Parameter_Amplitude,
            ParameterType.Parameter_Count
        };
        override public ParameterType[] AvailableParameters
        {
            get
            { return _AvailableParameters; }
        }

        private DeviceFunctionType[] _AvailableFunctions = new DeviceFunctionType[] {
             DeviceFunctionType.Function_Filter,
             DeviceFunctionType.Function_Thru
        };
        override public DeviceFunctionType[] AvailableFunctions
        {
            get
            { return _AvailableFunctions; }
        }

        override public void Reset()
        {
            prevSig = 0.0;
            waitingCount = 0.0;
            base.Reset();
        }

        double prevSig = 0.0;
        double waitingCount = 0.0;

        override public double SignalOut()
        {
            if (!Enabled)
                return 0.0;

            double signalOut = 0.0;
            double signalIn = 0.0;

            double DcOffset = Parameter_DC_Offset;
            double amplitude = Parameter_Amplitude;
            double frequency = 0.0;
            double phase = Parameter_Phase;

            double duty = Parameter_Duty;

            double count = Parameter_Count;

            DoModulationByChildDevices(ref signalOut, ref signalIn, ref amplitude, ref frequency, ref phase, ref duty);

            //----

            double sig = 0.0;

            if (count < 1)
                count = 1;

            switch (Function)
            {
                case DeviceFunctionType.Function_Filter:
                    if (waitingCount < 1.0)
                    {
                        sig = amplitude * signalIn;
                        prevSig = sig;
                    }
                    else
                    {
                        sig = prevSig;
                    }
                    waitingCount = (waitingCount + 1.0) % count;
                    break;

                case DeviceFunctionType.Function_Thru:
                    sig = amplitude * signalIn;
                    break;

                default:
                    break;
            }

            ++TimePosition;
            double SignalOut = sig + signalOut + DcOffset;
            SetPreviousSignalOut(SignalOut);
            return SignalOut;
        }
    }
}
